package com.qilu.pe.ui.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.dao.bean.RefreshEvent;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.ui.fragment.MyOrder1Fragment;

import org.greenrobot.eventbus.EventBus;

// 我的订单
public class MyOrderActivity extends Base2Activity {
    private XTabLayout tab_2;
    private ViewPager vp_tab;

    private MyOrderAdapter vpTabAdapter;
    private static final String[] HOME_MODELS = {"全部", "待付款", "已付款", "已取消", "退款"};

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        tab_2 = findViewById(R.id.tab_2);
        vp_tab = findViewById(R.id.vp_tab);

        setHeadTitle("我的订单");

        vpTabAdapter = new MyOrderAdapter(getSupportFragmentManager());
        vp_tab.setAdapter(vpTabAdapter);
        tab_2.setupWithViewPager(vp_tab);
        vp_tab.setOffscreenPageLimit(9);
        vp_tab.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                EventBus.getDefault().post(new RefreshEvent("refreshMyOrder", String.valueOf(position)));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private class MyOrderAdapter extends FragmentPagerAdapter {

        public MyOrderAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                //全部
                return MyOrder1Fragment.newInstance("0");
            } else if (position == 1) {
                return MyOrder1Fragment.newInstance("1");
            } else if (position == 2) {
                //已付款
                return MyOrder1Fragment.newInstance("2");
            } else if (position == 3) {
                return MyOrder1Fragment.newInstance("3");
            } else if (position == 4) {
                return MyOrder1Fragment.newInstance("4");
            }
            return MyOrder1Fragment.newInstance("0");
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return HOME_MODELS[position];
        }
    }
}